package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;
    /**
     * 检查项分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/selectAllByName")
    public PageResult selectAllByName(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 添加会员信息
     * @param member
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try{
            memberService.add(member);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    /**
     * 编辑会员信息
     * @param member
     * @return
     */
    @RequestMapping("/updateOne")
    public Result updateOne(@RequestBody Member member){
        try{
            memberService.edit(member);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            memberService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }
}
